package com.example.myapplication3;

import static kotlin.text.Typography.dollar;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class money_exchange_Activity extends AppCompatActivity {

TextView tv;

private static final String TAG="Rate";

    private float dollarRate=0.1f;
    private float euroRate=0.05f;
    private float wonRate=500f;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //EdgeToEdge.enable(this);
        setContentView(R.layout.activity_money_exchange);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        tv=findViewById(R.id.output_money);
    }

    public void onclick(View btn)
    {
        try {
            EditText et = findViewById(R.id.inputrmb);
            String input = et.getText().toString();
            float rmb = Float.parseFloat(input);
            float result = 0.0f;
            if (btn.getId() == R.id.dollar) {
                result = rmb * dollarRate;
            } else if (btn.getId() == R.id.euro) {
                result = rmb * euroRate;
            } else if (btn.getId() == R.id.won) {
                result = rmb * wonRate;
            }
            tv.setText("" + result);
            }
        catch (Exception ee) {

            Toast.makeText(this, "", Toast.LENGTH_LONG).show();
        }



    }
    public void clickOpen(View btn) {
        //打开新的窗口
        Intent config = new Intent(this, ConfigActivity.class);
        //传递参数
        config.putExtra("dollar_rate_key", dollarRate);
        config.putExtra("euro_rate_key", euroRate);
        config.putExtra("won_rate_key", wonRate);
        Log.i(TAG, "clickOpen: dollarRate=" + dollarRate);
        Log.i(TAG, "clickOpen: euroRate=" + euroRate);
        Log.i(TAG, "clickOpen: wonRate=" + wonRate);
        //startActivity(config);
        startActivityForResult(config,77);
    }
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data){
            if(requestCode==77 && resultCode==666){//from:startActivityForResult和setResult
                //汇率修改页面返回的数据，说明data中包含Bundle对象
                Bundle bdl = data.getExtras();
                        //拆分放入的数据

                dollarRate =bdl.getFloat("key_dollar2");
                euroRate = bdl.getFloat("key_euro2");
                wonRate = bdl.getFloat("key_won2");
                Log.i(TAG, "onActivityResult: dollarRate="+ dollarRate);
                Log.i(TAG,"onActivityResult: euroRate=" +euroRate);
                Log.i(TAG, "onActivityResult: wonRate=" + wonRate);
            }
        super.onActivityResult(requestCode, resultCode, data);
    }
}